from django.db import models
from django.contrib.auth.models import AbstractUser

# ------------ 用户表（继承Django内置用户模型） ------------
class User(AbstractUser):
    """
    扩展用户模型，增加状态字段
    - status: 用户状态（active/inactive）
    - created_at/updated_at: 自动记录时间戳
    """
    STATUS_CHOICES = (
        ('active', '在用'),
        ('inactive', '停用')
    )
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='active')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name

# ------------ 仓库表 ------------
class Warehouse(models.Model):
    """
    仓库信息表
    - name: 仓库名称（唯一）
    - status: 仓库状态（active/inactive）
    """
    name = models.CharField('仓库名称', max_length=100, unique=True)
    status = models.CharField(
        '状态', 
        max_length=10, 
        choices=(('active', '在用'), ('inactive', '停用')), 
        default='active'
    )
    created_at = models.DateTimeField('创建时间', auto_now_add=True)

    def __str__(self):
        return self.name

# ------------ 入库单表头 ------------
class InboundOrder(models.Model):
    """
    入库单主表
    - document_number: 单据编号（唯一约束）
    - status: 审核状态（pending/approved）
    - ForeignKey: 关联仓库、项目、录入人
    """
    document_number = models.CharField('单据编号', max_length=50, unique=True)
    document_date = models.DateField('单据日期')
    supplier = models.CharField('发货单位', max_length=100)
    warehouse = models.ForeignKey(
        Warehouse, 
        on_delete=models.PROTECT, 
        verbose_name='仓库'
    )
    project = models.ForeignKey(
        'Project',  # 项目表在下方定义
        on_delete=models.PROTECT, 
        verbose_name='关联项目'
    )
    entered_by = models.ForeignKey(
        User,
        on_delete=models.PROTECT,
        verbose_name='录入人'
    )
    status = models.CharField(
        '状态', 
        max_length=10, 
        choices=(('pending', '待审核'), ('approved', '已审核')), 
        default='pending'
    )
    created_at = models.DateTimeField('创建时间', auto_now_add=True)

    def __str__(self):
        return f"入库单-{self.document_number}"

# ------------ 入库单明细 ------------
class InboundOrderDetail(models.Model):
    """
    入库单明细表
    - quantity: 物料数量（支持小数）
    - ForeignKey: 关联主表和物料表
    """
    order = models.ForeignKey(
        InboundOrder, 
        on_delete=models.CASCADE, 
        related_name='details'
    )
    material = models.ForeignKey(
        'Material',  # 物料表在下方定义
        on_delete=models.PROTECT,
        verbose_name='物料'
    )
    quantity = models.DecimalField(
        '数量', 
        max_digits=10, 
        decimal_places=2
    )

    class Meta:
        verbose_name = '入库明细'
        verbose_name_plural = verbose_name

# ------------ 库存表 ------------
class Inventory(models.Model):
    """
    实时库存表
    - quantity: 当前数量
    - status: 物资状态（normal/scrap）
    - 唯一约束: 同一仓库+物料+状态只能有一条记录
    """
    warehouse = models.ForeignKey(
        Warehouse, 
        on_delete=models.CASCADE,
        verbose_name='仓库'
    )
    material = models.ForeignKey(
        'Material',
        on_delete=models.CASCADE,
        verbose_name='物料'
    )
    quantity = models.DecimalField(
        '数量', 
        max_digits=10, 
        decimal_places=2, 
        default=0
    )
    status = models.CharField(
        '状态', 
        max_length=10, 
        choices=(('normal', '正常'), ('scrap', '废旧')), 
        default='normal'
    )
    last_updated = models.DateTimeField('最后更新时间', auto_now=True)

    class Meta:
        verbose_name = '库存'
        verbose_name_plural = verbose_name
        # 唯一性约束（防止重复记录）
        unique_together = ('warehouse', 'material', 'status')

    def __str__(self):
        return f"{self.warehouse}-{self.material}"